<?php
	class DeliveryModel extends Model{
		//绑定用户
		function contuser($uname,$tname){
			$d_mongo = D("Mongo");
			
			//查找MOGODB内是否有这个用户
			$where["nickname"]=$uname;
			$where["teamname"]=$tname;
			$mogo_user=$d_mongo->_find($where,'delivery_user');//MOGODB查找用户
			if(!$mogo_user){
				$data_d=array(
					"nickname"=>$uname,
					"teamname"=>$tname,
					"addtime"=>time()
				);
				$d_mongo->add($data_d,'delivery_user');
			}
			
			$duser=M("delivery_user")->where(array("nickname"=>$uname))->find();//系统表查找用户
			$m_user=M("user")->where(array("nickname"=>$uname))->cache(true)->find();//联合表查找用户
			$dteam=M("delivery_team")->where(array("name"=>$tname))->cache(true)->find();//查找联合表
			
			if($duser){
				if($duser['user_id']==''){
					$up_data['user_id']=$m_user['user_id'];
				}
				if($duser['mid']==''){
					$up_data['mid']=$mogo_user[0]["_id"];
				}
				if($duser['teamid']==''){
					$up_data['teamid']=$dteam['id'];
				}
				
				M("delivery_user")->where(array("id"=>$duser['id']))->save($up_data);
				//dump($m_user);exit;
				return $duser["id"];
			}else{
				
				if($m_user){
					$datas['user_id']=$m_user['user_id'];
				}
				$mogo_user=$d_mongo->_find($where,'delivery_user');//MONGODB查找用户
				
				$datas['mid']=$mogo_user[0]["_id"];
				$datas['nickname']=$uname;
				$datas['teamid']=$dteam['id'];
				$datas['update']=time();
				$dcuser=M("delivery_user")->add($datas);
				return $dcuser;
			}
		}
		
		//生成查询团队数据
		function conteam($tname){
			$dteam=M("delivery_team")->where(array("name"=>$tname))->find();//查找联合表
			$mteam=M("department")->where(array("name"=>array('like', "%{$tname}%")))->cache(true)->find();//查找系统团队

			if(!$dteam){
				//系统团队不存在
				$datas['name']=$tname;
				$datas['addtime']=time();
				$datas['department_id']=$mteam['department_id'];
				$addteam=M("delivery_team")->add($datas);
				return $addteam;
			}else{
				if($dteam['department_id']==''){
					$dteams['department_id']=$mteam['department_id'];
				}
				M("delivery_team")->where(array("id"=>$dteam['id']))->save($dteams);
				return $dteam['id'];
			}
		}
		
		//配送员分页
		function page_user($where, $p, $limit=100){
			$user = M('Delivery_user');
			import('@.ORG.Page');
			/*foreach($where['teamid'] as $key=>$vw){
				$ks[0]='eq';
				$ks[1]=$vw;
				$where_a[]=$ks;
			}
			$where_a[]='or';*/
			if($where['teamid']){
				foreach($where['teamid'] as $vw){
					$wc[]=$vw;
				}
				$d['teamid']=$wc;
				/*$where_c=array(
					'_complex'=>$d,
					'_logic'=>'or'
				);*/
			}
			
			$count = $user->where($d)->count();
			$Page = new Page($count,$limit);
			$Page->parameter = '&t_start='.date('Y-m-d',$where['docid']['$gt']).'&t_end='.date('Y-m-d',$where['docid']['$lte']).'&teamid='.urlencode(json_encode($where['teamid']));
			$show  = $Page->show();
			$userlist = $user->where($d)->page($p.",100")->select();

			//集合数据
			foreach($userlist as $k=>$vo){
				$ss[$k]=$this->getUser_order($vo['nickname'],$where,$vo['id']);
				if($ss[$k]){
					$uorder[$k]=$ss[$k];
				}
			}
//				dump($where_c);exit;
			return array('page'=>$show ,'userlist'=>$uorder);
		}
		
		//查找单用户的指定时间单
		function getUser_order($uname,$where,$uid){
			unset($where['teamid']);
			$d_mongo = D("Mongo");
//			$user=F('user/'.$uid);
//			dump($user);
//			if(!$user){
//				unset($where);
				$where['psname']=$uname;
				$mogo_user=$d_mongo->_find($where,'delivery_detail');
//				F('user/'.$uid,$mogo_user);
//			}
			
			if(!$mogo_user){
				return false;
			}
//			$user_order['id']=$uid;
			foreach($mogo_user as $k=>$vo){
				$user_order["sum_value"]+=$vo['value'];
				switch($vo['status']){
					case 1:
						$user_order['status1'][$k]=$vo['status'];//正常
						break;
					case 2:
						$user_order['status2'][$k]=$vo['status'];//超时
						break;
					case 3:
						$user_order['status3'][$k]=$vo['status'];//异常
						break;
					case 0:
						$user_order['status0'][$k]=$vo['status'];//疑似欺诈
						break;
				}
				//违规
				switch($vo['vio']){
					case 1:
						$user_order['vio']+=$vo['vio'];
						break;
				}
				//基础配送费
				$user_order['base']+=$vo['base'];
				//提点
				$user_order['tidian']+=$vo['tidian'];
				//活动补贴K	
				$user_order['bt_hd']+=$vo['bt_hd'];
				//等级补贴L	
				$user_order['bt_dj']+=$vo['bt_dj'];
				//时效补贴M	
				$user_order['bt_sx']+=$vo['bt_sx'];
				//时效扣款N	
				$user_order['sx_kk']+=$vo['sx_kk'];
				//调度超时O	
				$user_order['ddcs']+=$vo['ddcs'];
				//异常扣款P
				$user_order['yckk']+=$vo['yckk'];
			}
			$user_order['psname']=$vo['psname'];
			$user_order['teamname']=$vo['teamname'];
			$user_order["sum"]=count($mogo_user);//总单量
			$user_order['status1']=count($user_order['status1']);
			$user_order['status2']=count($user_order['status2']);
			$user_order['status3']=count($user_order['status3']);
			$user_order['status0']=count($user_order['status0']);

//			dump($user_order);exit;
			
			return $user_order;
		}
		
		//查用户，为空就创建
		function user($nickname){
			$users=M("delivery_user")->where(array("nickname"=>$nickname))->cache(true)->find();
			if(!$users){
				$data['nickname']=$nickname;
				$data['update']=time();
				M("delivery_user")->add($data);
			}
		}
		
		function finds($where){
			$d_mongo = D("Mongo");
			$mogo_detail=$d_mongo->_find($where,'delivery_detail');
//			dump($mogo_detail);exit;
			foreach($mogo_detail as $k=>$vo){
				$this->user($vo['psname']);
				/*$tname=str_replace("可用可信","",$vo['teamname']);
				$tname=str_replace(' ','',$tname);
				$data=array(
					'teamname'=>$tname
				);
				$d_mongo->update(array("_id"=>$vo['_id']),$data,'delivery_detail');*/
				/*检查用户是否唯一
				 * 
				 if(!$vo['delivery_user']){
					$data['delivery_user']=$this->contuser($vo['psname'],$vo['teamname']);
				}elseif(!$vo['delivery_team']){0
					$data['delivery_team']=$this->conteam($vo['teamname']);
				}
				if($data){
					$d_mongo->update(array("_id"=>$vo['_id']),$data,'delivery_detail');
				}*/
			}
		}
		
		public function teamlist(){
			$team=M("delivery_team")->select();
			$m_user=M('user');
			foreach($team as $k=>$vo){
				$team[$k]['formula']=json_decode($vo['formula'],true);
				$user=$m_user->where(array('user_id'=>$vo['edit_user']))->find();
				$team[$k]['edit_user']=$user['nickname'];
			}
			return $team;
		}
		
		public function tidedit($tid){
			$team=M("delivery_team")->where(array("id"=>$tid))->find();
			/*foreach($team as $k=>$vo){
//				$team[$k]['name']=iconv('gbk','utf-8', $vo['name']);
//				$team[$k]['name']='';
//				$team[$k]['formula']=json_decode($vo['formula'],true);
				
			}*/
			$team['formula']=json_decode($team['formula'],true);
			return $team;
		}
		
		public function arr2($cha,$arr){
			$ret=false;
			foreach($arr as $vo){
				if(in_array($cha,$vo)){
					$ret= true;
					break;
				}
			}
			return $ret;
		}
		
	}
?>